﻿package export.strength
{
	import com.dusk.util.RandomUtil;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.EquipmentVO;
	import com.game.data.vo.PlayerDataVO;
	import com.game.event.GameEvent;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.module.backpack.vo.BackPackEvent;
	import com.game.view.component.icon.RoleEquipIcon;
	
	import config.Config;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class Fusion extends Sprite
	{
		
		public function Fusion(param1:PlayerDataVO)
		{
			super();
			TextUtil.redrawTxtOld(this, this.needRedrawText);
			this.threeObj = [];
			this.player = param1;
			this.gc = Config.getIns();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var rlBtn:SimpleButton;
		public var helpBtn:SimpleButton;
		public var material1:Sprite;
		public var material2:Sprite;
		public var material3:Sprite;
		public var produce:Sprite;
		public var preview:Sprite;
		public var txt_name:TextField;
		public var txt_needlh:TextField;
		public var txt_success:TextField;
		private var gc:Config;
		private var player:PlayerDataVO;
		private var produceEquipName:String = "";
		private var threeObj:Array;
		private var outList:Array = [];
		private var needRedrawText:Array = ["txt_name", "txt_needlh", "txt_success"];
		
		public function setRole(param1:PlayerDataVO):void
		{
			this.player = param1;
		}
		
		private function removedProduceEquipment():void
		{
			var _loc1_:* = null;
			if (this.produce.getChildByName("curzb") != null)
			{
				_loc1_ = this.produce.getChildByName("curzb") as RoleEquipIcon;
				this.produce.removeChild(_loc1_);
				_loc1_ = null;
			}
		}
		
		private function previewFun():void
		{
			var _loc1_:* = undefined;
			this.txt_name.text = "";
			if (this.preview.getChildByName("view") != null)
			{
				this.preview.removeChild(this.preview.getChildByName("view"));
			}
			this.getThreeObj();
			var _loc2_:Array = this.gc.allEquip.mixProduce(this.threeObj);
			this.produceEquipName = _loc2_[0];
			if (this.produceEquipName != "")
			{
				_loc1_ = ResUtil.getImageBitmap(this.produceEquipName);
				_loc1_.name = "view";
				this.preview.addChild(_loc1_);
				this.txt_name.text = _loc2_[1];
				this.txt_needlh.text = "2000";
				this.txt_success.text = _loc2_[2] + "%";
			}
		}
		
		private function removeEquipFormBack(param1:EquipmentVO, param2:int):void
		{
			var _loc3_:int = 0;
			if (param2 == 1)
			{
				if (param1.num > 1)
				{
					param1.modifyNum(-1);
				}
				else
				{
					_loc3_ = this.player.bagEquipList.indexOf(param1);
					if (_loc3_ != -1)
					{
						this.player.bagEquipList.splice(_loc3_, 1);
					}
				}
			}
			else if (param2 == 2)
			{
				if (param1.num > 1)
				{
					param1.modifyNum(-1);
				}
				else
				{
					_loc3_ = this.player.bagStuffList.indexOf(param1);
					if (_loc3_ != -1)
					{
						this.player.bagStuffList.splice(_loc3_, 1);
					}
				}
			}
			else if (param2 == 3)
			{
				if (param1.num > 1)
				{
					param1.modifyNum(-1);
				}
				else
				{
					_loc3_ = this.player.bugConsumeList.indexOf(param1);
					if (_loc3_ != -1)
					{
						this.player.bugConsumeList.splice(_loc3_, 1);
					}
				}
			}
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
		}
		
		private function getProduce():void
		{
			var _loc1_:* = this.produce.getChildByName("curzb") as RoleEquipIcon;
			if (_loc1_ != null)
			{
				if (_loc1_.getMyEquipObj().type == "zbfb" || _loc1_.getMyEquipObj().type == "zbwq" || _loc1_.getMyEquipObj().type == "zbfj" || _loc1_.getMyEquipObj().type == "zbsp" || _loc1_.getMyEquipObj().type == "zbtx")
				{
					this.player.bagEquipList.push(_loc1_.getMyEquipObj());
				}
				else if (_loc1_.getMyEquipObj().type == "wpqhs" || _loc1_.getMyEquipObj().type == "zbwp")
				{
					this.gc.putQhsInBackPack(this.player, _loc1_.getMyEquipObj().equipId);
				}
				else if (_loc1_.getMyEquipObj().type == "zbsz")
				{
					this.player.bugConsumeList.push(_loc1_.getMyEquipObj());
				}
				_loc1_ = null;
			}
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
		}
		
		private function getThreeObj():void
		{
			var _loc1_:* = null;
			this.threeObj = [];
			this.outList = [];
			var _loc2_:int = 0;
			while (_loc2_ < 3)
			{
				if (this["material" + (_loc2_ + 1)].getChildByName("curzb"))
				{
					_loc1_ = this["material" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					this.threeObj.push(_loc1_.getMyEquipObj());
					this.outList.push({
						"dequip": _loc1_.getMyEquipObj(),
						"count" : 1
					});
					_loc1_ = null;
				}
				_loc2_++;
			}
		}
		
		private function removeThreeObj():void
		{
			var _loc4_:Date = null;
			var _loc1_:* = null;
			var _loc2_:* = null;
			this.threeObj = [];
			this.outList = [];
			this.txt_success.text = "";
			var _loc3_:* = "";
			var _loc5_:int = (_loc4_ = new Date()).getDate();
			var _loc6_:int = _loc4_.getHours();
			var _loc7_:int = _loc4_.getMinutes();
			var _loc8_:String = _loc5_ + "-" + _loc6_ + "-" + _loc7_;
			var _loc9_:int = 0;
			while (_loc9_ < 3)
			{
				if (this["material" + (_loc9_ + 1)].getChildByName("curzb"))
				{
					_loc1_ = this["material" + (_loc9_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					_loc2_ = _loc1_.getMyEquipObj().equipId;
					if (_loc2_.indexOf("wpqhs") != -1)
					{
						_loc3_ += _loc2_.substr(5, 1);
						if (_loc9_ < 2)
						{
							_loc3_ += ",";
						}
					}
					_loc1_ = this["material" + (_loc9_ + 1)].removeChild(_loc1_);
					_loc1_ = null;
				}
				_loc9_++;
			}
			/*if (_loc3_ != "")
			{
				if (this.gc.qhsInfo.length >= 10)
				{
					this.gc.qhsInfo.shift();
				}
				this.gc.qhsInfo.push(_loc8_ + "|" + _loc3_ + "|2");
			}*///以后可能可以用来处理成强化幸运值满值时有概率加成
		}
		
		private function getAndRemovedEquip():void
		{
			this.removeThreeObj();
			this.getProduce();
		}
		
		private function doFusion():void
		{
			var _loc1_:* = null;
			var _loc2_:* = null;
			trace("---produceEquipName---" + this.produceEquipName);
			this.rlBtn.enabled = false;
			if (this.produceEquipName != "")
			{
				if (this.player.lhValue < int(this.txt_needlh.text))
				{
					NotificationManager.alert("灵魂值不够！");
					this.rlBtn.enabled = true;
					return;
				}
				player.reduceLhValue(int(this.txt_needlh.text));
				if (!RandomUtil.randBoolean(int(this.txt_success.text.replace("%", ""))))
				{
					NotificationManager.alert("合成失败！");
					this.rlBtn.enabled = true;
					StrengthEquipment(this.parent).setTxtlh();
					return;
				}
				if (this.produceEquipName.indexOf("wpqhs") != -1 || this.produceEquipName.indexOf("sms") != -1 || this.produceEquipName.indexOf("mfs") != -1 || this.produceEquipName.indexOf("gjs") != -1 || this.produceEquipName.indexOf("fys") != -1)
				{
					this.afterReadStore();
				}
				else
				{
					StrengthEquipment(this.parent).setTxtlh();
					var type:String = this.gc.allEquip.getTypeByName(this.produceEquipName);
					if (this.produceEquipName.indexOf("zzs") != -1)
					{
						_loc1_ = this.gc.allEquip.findByName(this.produceEquipName);
					}//制作书直接获取初始值
					else if (this.produceEquipName == "_dzj" || this.produceEquipName == "dzjj" || this.produceEquipName == "hy")
					{
						_loc1_ = this.gc.allEquip.getSunSutraValueEquip(this.produceEquipName, this.threeObj);
					}//地藏戒，地藏金戒，花宴继承属性
					else if (type == "zbfb" || type == "zbsz")
					{
						_loc1_ = this.gc.allEquip.findByName(this.produceEquipName);
					}//法宝直接获取初始值，减小差距，降低肝度;时装直接获取初始值
					else if (this.produceEquipName.indexOf("sq") != -1)
					{
						_loc1_ = this.gc.allEquip.getShenzhuValue(this.produceEquipName, this.threeObj);
					}//通过合成实现重铸功能
					else
					{
						_loc1_ = this.gc.allEquip.getSutraValue(this.produceEquipName, this.threeObj);
					}
					_loc2_ = new RoleEquipIcon(_loc1_);
					this.produce.addChild(_loc2_);
					NotificationManager.alert("合成成功");
					this.gc.eventManager.dispatchEvent(new GameEvent("RefreshLHValue"));
					this.produceEquipName = "";
					this.getAndRemovedEquip();
				}
			}
			this.rlBtn.enabled = true;
			if (this.preview.getChildByName("view") != null)
			{
				this.preview.removeChild(this.preview.getChildByName("view"));
			}
		}
		
		private function afterReadStore():void
		{
			var _loc1_:EquipmentVO = this.gc.allEquip.findByName(this.produceEquipName);
			var _loc2_:RoleEquipIcon = new RoleEquipIcon(_loc1_);
			this.produce.addChild(_loc2_);
			NotificationManager.alert("合成成功");
			this.produceEquipName = "";
			this.getAndRemovedEquip();
		}
		
		private function removedSelf():void
		{
			var _loc1_:* = null;
			this.removedProduceEquipment();
			var _loc2_:int = 0;
			while (_loc2_ < 3)
			{
				if (this["material" + (_loc2_ + 1)].getChildByName("curzb"))
				{
					_loc1_ = this["material" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					this["material" + (_loc2_ + 1)].removeChild(_loc1_);
					if (_loc1_.getMyEquipObj().type == "zbwq" || _loc1_.getMyEquipObj().type == "zbfj" || _loc1_.getMyEquipObj().type == "zbsp" || _loc1_.getMyEquipObj().type == "zbtx" || _loc1_.getMyEquipObj().type == "zbfb")
					{
						this.player.bagEquipList.push(_loc1_.getMyEquipObj());
					}
					else if (_loc1_.getMyEquipObj().type == "wpqhs" || _loc1_.getMyEquipObj().type == "zbwp")
					{
						this.gc.putQhsInBackPack(this.player, _loc1_.getMyEquipObj().equipId);
					}
					else if (_loc1_.getMyEquipObj().type == "zbsz")
					{
						this.player.bugConsumeList.push(_loc1_.getMyEquipObj());
					}
					_loc1_ = null;
				}
				_loc2_++;
			}
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK)
		}
		
		private function added(param1:Event):void
		{
			this.outList = [];
			this.gc.eventManager.addEventListener("SimpleClick", this.receiveObj);
			this.rlBtn.addEventListener(MouseEvent.CLICK, this.makeClick);
			this.helpBtn.addEventListener(MouseEvent.CLICK, this.showHelp);
			this.material1.addEventListener(MouseEvent.CLICK, this.onClick);
			this.material2.addEventListener(MouseEvent.CLICK, this.onClick);
			this.material3.addEventListener(MouseEvent.CLICK, this.onClick);
			this.produce.addEventListener(MouseEvent.CLICK, this.produceClick);
		}
		
		private function removed(param1:Event):void
		{
			this.outList = [];
			this.gc.eventManager.removeEventListener("SimpleClick", this.receiveObj);
			this.rlBtn.removeEventListener(MouseEvent.CLICK, this.makeClick);
			this.helpBtn.removeEventListener(MouseEvent.CLICK, this.showHelp);
			this.material1.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.material2.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.material3.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.produce.removeEventListener(MouseEvent.CLICK, this.produceClick);
			this.removedSelf();
		}
		
		private function showHelp(param1:MouseEvent):void
		{
			new FusionHelp();
		}
		
		private function onClick(param1:MouseEvent):void
		{
			var _loc2_:RoleEquipIcon = null;
			if (param1.currentTarget.getChildByName("curzb") != null)
			{
				_loc2_ = param1.currentTarget.getChildByName("curzb") as RoleEquipIcon;
				param1.currentTarget.removeChild(_loc2_);
				switch (_loc2_.getEquipVO().type)
				{
					case "zbwq":
					case "zbfj":
					case "zbsp":
					case "zbfb":
					case "zbtx":
						if (this.gc.allEquip.isTheSameEquipmentByFillName(_loc2_.getEquipVO()))
						{
							this.player.bagEquipList.push(_loc2_.getEquipVO());
						}
						else
						{
							NotificationManager.alert("无法找到合成项目");
						}
						break;
					case "zbsz":
						this.player.bugConsumeList.push(_loc2_.getEquipVO());
						break;
					case "wpqhs":
						if (_loc2_.getEquipVO().name.indexOf("强化石") != -1)
						{
							this.gc.putQhsInBackPack(this.player, _loc2_.getEquipVO().equipId);
							break;
						}
						break;
					case "zbwp":
						this.gc.putQhsInBackPack(this.player, _loc2_.getEquipVO().equipId);
				}
				_loc2_ = null;
			}
			this.previewFun();
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
		}
		
		private function produceClick(param1:MouseEvent):void
		{
			this.removedProduceEquipment();
		}
		
		private function receiveObj(param1:GameEvent):void
		{
			var _loc2_:RoleEquipIcon = param1.data[0] as RoleEquipIcon;
			switch (_loc2_.getEquipVO().type)
			{
				case "zbwq":
				case "zbfj":
				case "zbsp":
				case "zbfb":
				case "zbtx":
				case "":
					if (this.material1.numChildren < 2)
					{
						this.material1.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 1);
						break;
					}
					if (this.material2.numChildren < 2)
					{
						this.material2.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 1);
						break;
					}
					if (this.material3.numChildren < 2)
					{
						this.material3.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 1);
						break;
					}
					break;
				case "zbsz":
					if (this.material1.numChildren < 2)
					{
						this.material1.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 3);
						break;
					}
					if (this.material2.numChildren < 2)
					{
						this.material2.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 3);
						break;
					}
					if (this.material3.numChildren < 2)
					{
						this.material3.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 3);
						break;
					}
					break;
				case "wpqhs":
				case "zbwp":
					if (this.material1.numChildren < 2)
					{
						this.material1.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 2);
						break;
					}
					if (this.material2.numChildren < 2)
					{
						this.material2.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 2);
						break;
					}
					if (this.material3.numChildren < 2)
					{
						this.material3.addChild(_loc2_);
						this.removeEquipFormBack(_loc2_.getEquipVO(), 2);
						break;
					}
					break;
			}
			this.previewFun();
			this.removedProduceEquipment();
		}
		
		private function makeClick(param1:MouseEvent):void
		{
			this.doFusion();
		}
	}
}
